package com.sgb.goods.view.adapter.publish.pop;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.databinding.ObservableField;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.widget.pop.GoodsPopWindow;
import com.swgk.core.dialog.PopViewHolder;

public class FuturesPopWindow extends GoodsPopWindow implements View.OnClickListener {

    private TextView textDay, textMoth, okView, resetView;
    private EditText inputData;
    private RadioButton radioButtonLeft, radioButtonRight;
    private LinearLayout rightLayout;
    private boolean leftSelect = true, rightSelect, isSelectDay = true;
    private String dataTxt;
    /** 输入的日期 */
    public ObservableField<String> contentText = new ObservableField<>("");

    public FuturesPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.goods_pop_futures;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        holder.setBinding(BR.viewModel, this);
        inputData = holder.getPop().findViewById(R.id.input_data);
        textDay = holder.getPop().findViewById(R.id.time_day);
        textMoth = holder.getPop().findViewById(R.id.time_month);
        radioButtonLeft = holder.getPop().findViewById(R.id.r1);
        radioButtonRight = holder.getPop().findViewById(R.id.r2);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        rightLayout = holder.getPop().findViewById(R.id.right_layout);
        radioButtonLeft.setOnClickListener(this);
        radioButtonRight.setOnClickListener(this);
        textMoth.setOnClickListener(this);
        textDay.setOnClickListener(this);
        okView.setOnClickListener(this);
        resetView.setOnClickListener(this);
        setSelectLeftCheck();
        inputData.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!rightSelect) {
                    return;
                }
                if (TextUtils.isEmpty(s)) {
                    inputData.setHint("请输入商品货期");
                    return;
                }
                if ("0".equals(s.toString())) {
                    inputData.setText("");
                    return;
                }
                if (isSelectDay) {
                    int day = Integer.parseInt(s.toString());
                    if (day > 365) {
                        ToastUtil.show(mContext, "最大值不超过365天");
                        inputData.setText("365");
                    }
                } else {
                    int moth = Integer.parseInt(s.toString());
                    if (moth > 36) {
                        ToastUtil.show(mContext, "最大值不超过36个月");
                        inputData.setText("36");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.r1) {
            leftSelect = !leftSelect;
            setSelectLeftCheck();
        } else if(v.getId() == R.id.r2) {
            rightSelect = !rightSelect;
            setSelectRightCheck();
        } else if(v.getId() == R.id.time_day) {
            isSelectDay = true;
            setDayCheck();
        } else if(v.getId() == R.id.time_month) {
            isSelectDay = false;
            setDayCheck();
            if (!TextUtils.isEmpty(inputData.getText().toString()) && Integer.parseInt(inputData.getText().toString()) > 36) {
                ToastUtil.show(mContext, "最大值不超过36个月");
                inputData.setText("");
            }
        } else if(v.getId() == R.id.ok_view) {
            if (onResultClick != null) {
                if (rightSelect) {
                    dataTxt = inputData.getText().toString();
                }
                if (leftSelect){
                    dataTxt = "现货";
                }
                if (!rightSelect&&!leftSelect){
                    dataTxt = "";
                }
                if (rightSelect && TextUtils.isEmpty(dataTxt)) {
                    ToastUtil.show(mContext, "请输入货期时间");
                    return;
                } else {
                    dissMiss();
                    if (leftSelect) {
                        onResultClick.result(dataTxt, leftSelect, null);
                    } else {
                        if (rightSelect) {
                            onResultClick.result(dataTxt, leftSelect, isSelectDay);
                        } else {
                            onResultClick.result("", leftSelect, isSelectDay);
                        }
                    }
                }
            }
        } else if(v.getId() == R.id.reset_view) {
            cancel();
            dismiss();
        }
    }

    private void cancel(){
        if (TextUtils.isEmpty(dataTxt)){
            if (leftSelect){
                leftSelect = false;
                setSelectLeftCheck();
            }
            if (rightSelect){
                rightSelect = false;
                setSelectRightCheck();
                inputData.setText("");
            }
        }else {
            if ("现货".equals(dataTxt)){
                leftSelect = true;
                setSelectLeftCheck();
            }else {
                rightSelect = true;
                setSelectRightCheck();
            }
        }
    }

    public void setPopData(String dataTxt, boolean leftSelect, boolean isSelectDay) {
        this.dataTxt = dataTxt;
        this.leftSelect = leftSelect;
        this.isSelectDay = isSelectDay;
        inputData.setText(dataTxt);
        if (leftSelect) {
            setSelectLeftCheck();
        } else {
            this.rightSelect = true;
            setSelectRightCheck();
        }
    }

    private void setSelectLeftCheck() {
        if (leftSelect) {
            rightSelect = false;
            radioButtonLeft.setChecked(true);
            radioButtonRight.setChecked(false);
            rightLayout.setVisibility(View.GONE);
        } else {
            radioButtonLeft.setChecked(false);
        }
    }

    private void setSelectRightCheck() {
        if (rightSelect) {
            leftSelect = false;
            radioButtonLeft.setChecked(false);
            radioButtonRight.setChecked(true);
            setDayCheck();
            rightLayout.setVisibility(View.VISIBLE);
        } else {
            radioButtonRight.setChecked(false);
            rightLayout.setVisibility(View.GONE);
        }
    }

    IDisMiss iDisMiss;

    public interface IDisMiss {
        void OnClose();
    }

    public void OnDisMiss(IDisMiss iDisMiss) {
        this.iDisMiss = iDisMiss;
    }

    private void setDayCheck() {
        if (isSelectDay) {
            textDay.setCompoundDrawablesRelativeWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.multi_select_switch_pressed), null, null, null);
            textMoth.setCompoundDrawablesRelativeWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.multi_select_switch_normal), null, null, null);
        } else {
            textMoth.setCompoundDrawablesRelativeWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.multi_select_switch_pressed), null, null, null);
            textDay.setCompoundDrawablesRelativeWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.multi_select_switch_normal), null, null, null);
        }
    }
}
